package gdut.imis.controller;

import gdut.imis.Utils.Result;
import gdut.imis.domain.Manager;
import gdut.imis.domain.User;
import gdut.imis.service.ManagerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * @Description
 * @Author JiaFu
 * @Date 2023/4/8 18:28
 */
@RestController
@RequestMapping("/manager")
@Api(value = "管理员接口", tags = {"管理员接口"})
@RequiredArgsConstructor
public class ManagerController extends BaseController {
    private final ManagerService managerService;

    @ApiOperation("新增管理员")
    @PostMapping("/addManager")
    @PreAuthorize("@cp.hasOneOfPermissions('ONE_LEVAL_MANAGER','TWO_LEVAL_MANAGER')")
    public Result add(@RequestBody Manager manager) {
        return managerService.addManager(manager);
    }

    @ApiOperation("查询管理员基本信息")
    @GetMapping("/getManagerInfo")
    public Result getManagerInfo() {
        return managerService.getManagerInfo();
    }

    @ApiOperation("头像上传(通用)")
    @PostMapping("/uploadAvatar")
    public Result uploadAvatar(MultipartFile file) throws IOException {
        return managerService.uploadAvatar(file);
    }

    @ApiOperation("头像下载(通用)")
    @GetMapping("/downloadAvatar")
    public void downloadAvatar(String avatarUrl, HttpServletResponse response) throws FileNotFoundException {
        managerService.downloadAvatar(avatarUrl, response);
    }

    @ApiOperation("更新管理员信息")
    @PutMapping("/updateManagerInfo")
    public Result updateMangerInfo(@RequestBody Manager manager) {
        return managerService.updateManagerInfo(manager);
    }

    @ApiOperation("根据管理员类型来查询管理员列表")
    @GetMapping("/getManagerListByType/{type}")
    @ApiImplicitParam(name = "type", value = "管理员类型(0：一级管理员，可创建学校一级管理员；1：二级管理员，可创建学校二级管理员；2：二级管理员，可以授予教师学生相应的档案录入权限）)", dataType = "Int", required = true)
    @PreAuthorize("@cp.hasOneOfPermissions('ONE_LEVAL_MANAGER,TWO_LEVAL_MANAGER')")
    public Result getManagerListByType(
            @PathVariable("type") Integer type,
            @RequestParam("pageNo") Integer pageNo,
            @RequestParam("pageSize") Integer pageSize,
            @RequestParam(value = "universityId", required = false) Long universityId) {
        Result result = this.checkPageParams(pageNo, pageSize);
        if (result.getCode() == 0) {
            return managerService.getPageManagerListByType(type, pageNo, pageSize, universityId);
        }
        return result;
    }

    @ApiOperation("根据id删除管理员")
    @DeleteMapping("/deleteManagerById/{id}")
    @PreAuthorize("@cp.hasOneOfPermissions('ONE_LEVAL_MANAGER','TWO_LEVAL_MANAGER')")
    public Result deleteManagerById(@PathVariable("id") Integer id) {
        return managerService.deleteManagerById(id);
    }

    @ApiOperation("根据学校id获取该校所有教师信息")
    @GetMapping("/getPageTeachersByUniversityId/{id}/{pageNo}/{pageSize}")
    @PreAuthorize("@cp.hasPermission('THREE_LEVAL_MANAGER')")
    public Result getPageTeachersByUniversityId(
            @PathVariable("id") Long id,
            @PathVariable("pageNo") Integer pageNo,
            @PathVariable("pageSize") Integer pageSize) {
        Result result = this.checkPageParams(pageNo, pageSize);
        if (result.getCode() == 0) {
            return managerService.getPageTeachersByUniversityId(id, pageNo, pageSize);
        }
        return result;
    }

    @ApiOperation("新增教师（type:1）")
    @PostMapping("/addTeacherUser")
    @PreAuthorize("@cp.hasPermission('THREE_LEVAL_MANAGER')")
    public Result addTeacher(@RequestBody User user) {
        return managerService.addTeacherUser(user);
    }

    @ApiOperation(("根据用户id删除教师（三级管理员才有的权限）"))
    @DeleteMapping("/deleteTeacherUserById/{id}")
    @PreAuthorize("@cp.hasPermission('THREE_LEVAL_MANAGER')")
    public Result deleteTeacherUserById(@PathVariable("id") Long userId) {
        return managerService.deleteTeacherUserById(userId);
    }

    @ApiOperation("下载档案录入权限申请的证明材料文件（三级管理员才有的权限）")
    @GetMapping("/downloadProveFile")
//    @PreAuthorize("@cp.hasPermission('THREE_LEVAL_MANAGER')")
    public void downloadProveFile(@RequestParam("url") String url, HttpServletResponse response) throws IOException {
        managerService.downloadProveFile(url, response);
    }


}
